package com.chengliang.bbs.controller;

import com.chengliang.bbs.entry.ContinuityPractice;
import com.chengliang.bbs.entry.JSONResult;
import com.chengliang.bbs.entry.UserAuths;
import com.chengliang.bbs.service.ContinuityPracticeService;
import com.chengliang.bbs.service.PracticeService;
import com.chengliang.bbs.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户（修炼）签到
 * @author: chengliang
 * @create: 2019-07-31 00:20
 */
@RestController
@RequestMapping("/sign")
@Slf4j
public class SignController {

    private final PracticeService practiceService;

    private final ContinuityPracticeService continuityPracticeService;

    @Autowired
    public SignController(PracticeService practiceService, ContinuityPracticeService continuityPracticeService) {
        this.practiceService = practiceService;
        this.continuityPracticeService = continuityPracticeService;
    }

    @PostMapping("/in")
    public JSONResult in(){
        //检查是否已登录
        UserAuths userAuths = UserUtils.currentUser();
        if(userAuths == null){
            return JSONResult.error("请登录后进行修炼");
        }
        //当前用户ID
        Integer userId = userAuths.getUser().getUserId();
        //今日是否已签到
        if(practiceService.isNowDayPracticed(userId)){
            return JSONResult.error("今日已签到");
        }
        //签到
        boolean b = practiceService.practiceByUserId(userId);
        if(!b){
            return JSONResult.error("修炼失败,请重新试一下");
        }
        //进行签到
        return JSONResult.success(continuityPracticeService.getContinuityPracticeByUserId(userId));
    }

    /**
     * 签到(修炼)状态
     * @return
     * 前端js中无法直接使用thymleaf标签，因此从后台接口获取登录状态
     */
    @PostMapping("/status")
    public JSONResult status(){
        UserAuths userAuths = UserUtils.currentUser();
        if(userAuths == null){
            return JSONResult.success();
        }
        return JSONResult.success(continuityPracticeService.getContinuityPracticeByUserId(userAuths.getUserId()));
    }

    @GetMapping("/top20List")
    public JSONResult top_20List(){
        //连续签到topList
        List<ContinuityPractice> highestPracticeTop20 = continuityPracticeService.getHighestPracticeTop20();
        //最早
        List<ContinuityPractice> earliestPracticeTop20 = continuityPracticeService.getEarliestPracticeTop20();
        //最新
        List<ContinuityPractice> newestPracticeTop20 = continuityPracticeService.getNewestPracticeTop20();
        log.info("最新签到信息:{}",newestPracticeTop20);
        //存储到集合中
        List<List> topAllList=new ArrayList<>();
        topAllList.add(newestPracticeTop20);
        topAllList.add(earliestPracticeTop20);
        topAllList.add(highestPracticeTop20);
        return JSONResult.success(topAllList);
    }


}
